package com.ks.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ks.handler.BusinessException;
import com.ks.response.Result;
import com.ks.response.ResultCode;
import com.ks.system.entity.Role;
import com.ks.system.service.RoleService;
import com.ks.system.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author ks
 * @since 2020-12-01
 */
@Api(value = "系统用户角色模块", tags = "系统用户角色接口")
@RestController
@RequestMapping("/system/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 简化的分页查询
     * @param current
     * @param size
     * @param roleName
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/findAll")
    public Result findAll(@RequestParam(required = true, defaultValue = "1") Integer current,
                              @RequestParam(required = true, defaultValue = "6") Integer size,
                              String roleName) {
        Page<Role> page = roleService.getRoleList(roleName, new Page<>(current, size));
        return Result.ok().data("page", page);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据角色ID进行查询",notes = "根据角色ID进行查询")
    @GetMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.getBaseMapper().selectById(id);
        if(role!=null){
            return Result.ok().data("role", role);
        }else{
            throw new BusinessException(ResultCode.ROLE_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.ROLE_ACCOUNT_NOT_EXIST.getMessage());
        }
    }

    /**
     * 添加用户角色
     * @param role
     * @return
     */
    @ApiOperation(value = "添加用户角色",notes = "添加用户角色")
    @PostMapping(value = "/addRole")
    public Result addRole(@RequestBody Role role){
        try {
            roleService.addRole(role);
            return Result.ok();
        } catch (Exception e) {
            //打印错误日志
            return Result.error();
        }
    }

    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @ApiOperation(value = "修改用户角色信息",notes = "修改用户角色信息")
    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody Role role) {
        try {
            roleService.getBaseMapper().updateById(role);
            return Result.ok();
        } catch (Exception e) {
            //打印错误日志
            return Result.error();
        }
    }

    /**
     * 根据角色id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除用户角色信息",notes = "根据用户角色id删除信息")
    @DeleteMapping("/delRole")
    public Result delRole(Integer id){
        try {
            roleService.getBaseMapper().deleteById(id);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 导出用户角色信息
     * @param response
     * @param roleName
     * @throws IOException
     */
    @ApiOperation(value = "导出用户角色信息")
    @PostMapping(value = "/export")
    public void exportRoleExcel(HttpServletResponse response, String roleName) throws IOException {
        QueryWrapper wrapper = new QueryWrapper();
        if(roleName != null){
            wrapper.like("role_name", roleName);
        }
        List<Role> roles = roleService.list(wrapper);
        roleService.exportUserExcel(response,roles);
    }

}

